//////////////////////////////////////////////////////////////////////////
//
// Vector Graph Core2D
//
// Module:
//        Rotate2D.cs
//
// Description:
//        Implementation of rotate operation
//
// Author:
//        Created on 2008.04.17 by Destrayer
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace VectorGraph.Core2D
{
    class Rotate2D
    {
        public static void Rotate(ref Point2d pt, Point2d basePt, double cosAngle, double sinAngle)
        {
            pt -= basePt;
            double x = pt.X;
            pt.X = x * cosAngle - pt.Y * sinAngle;
            pt.Y = x * sinAngle + pt.Y * cosAngle;
            pt += basePt;
        }

        public static void Rotate(ref Point2d pt, Point2d basePt, double angle)
        {
            Rotate2D.Rotate(ref pt, basePt, Math.Cos(angle), Math.Sin(angle));
        }

        public static void Rotate(Point2d[] pts, Point2d basePt, double angle)
        {
            double cosAngle = Math.Cos(angle);
            double sinAngle = Math.Sin(angle);
            for (int i = pts.Length - 1; i >= 0; i--)
            {
                Rotate2D.Rotate(ref pts[i], basePt, cosAngle, sinAngle);
            }
        }

        public static void Rotate(ref Beeline2d bl, Point2d basePt, double angle)
        {
            Rotate2D.Rotate(ref bl.BasePt, basePt, angle);
            Rotate2D.Rotate(ref bl.Direction, new Point2d(), angle);
        }

        public static void Rotate(ref Circle2d c, Point2d basePt, double angle)
        {
            Rotate2D.Rotate(ref c.CenterPt, basePt, angle);
        }

        public static void Rotate(ref Segment2d seg, Point2d basePt, double angle)
        {
            Rotate2D.Rotate(ref seg.StartPt, basePt, angle);
            Rotate2D.Rotate(ref seg.EndPt, basePt, angle);
        }

        public static void Rotate(ref Arc2d arc, Point2d basePt, double angle)
        {
            Rotate2D.Rotate(ref arc.CenterPt, basePt, angle);
            Rotate2D.Rotate(ref arc.StartVector, new Point2d(), angle);
            Rotate2D.Rotate(ref arc.EndVector, new Point2d(), angle);
        }

        public static void Rotate(ref Path2d path, Point2d basePt, double angle)
        {
            Point2d[] pts = new Point2d[path.Count];
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] = path.Vertex(i);
            }
            Rotate2D.Rotate(pts, basePt, angle);
            for (int i = 0; i < pts.Length; i++)
            {
                path.SetVertex(i, pts[i]);
            }
        }

        public static void Rotate(ref Polygon2d poly, Point2d basePt, double angle)
        {
            Point2d[] pts = new Point2d[poly.Count];
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] = poly.Vertex(i);
            }
            Rotate2D.Rotate(pts, basePt, angle);
            for (int i = 0; i < pts.Length; i++)
            {
                poly.SetVertex(i, pts[i]);
            }
        }
    }
}
